<template>
	<view class="container">
		<view class="login-box">
			<view class="logo-section">
				<image class="logo" src="/static/logo.png"></image>
				<text class="title">用户登录</text>
			</view>
			
			<view class="input-section">
				<view class="input-item">
					<text class="input-label">手机号</text>
					<input class="input" type="number" placeholder="请输入手机号" v-model="formData.phone" />
				</view>
				
				<view class="input-item">
					<text class="input-label">密码</text>
					<input class="input" type="password" placeholder="请输入密码" v-model="formData.password" />
				</view>
			</view>
			
			<button class="login-button" @click="handleLogin">登录</button>
			
			<view class="footer">
				<text class="link" @click="goToRegister">立即注册</text>
				<text class="link" @click="forgetPassword">忘记密码？</text>
			</view>
		</view>
	</view>
</template>

<script>
	import { login } from '../../common/api.js';
	import websocket from '@/common/websocket.js'

	export default {
		data() {
			return {
				formData: {
					phone: '',
					password: ''
				}
			}
		},
		methods: {
			async handleLogin() {
				// 简单验证
				if (!this.formData.phone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
					return;
				}
				
				if (!this.formData.password) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
					return;
				}
				
				// 调用登录接口
				try {
					const res = await login({
						account: this.formData.phone,
						password: this.formData.password
					});
					
					// 添加调试日志
					console.log('登录接口返回:', res);
					
					// 检查后端返回的数据，确认登录是否成功
					// 根据日志，后端使用 code=0 表示成功，其他值表示失败
					if (res.code !== 0) {
						uni.showToast({
							title: res.msg || '登录失败，请检查账号密码',
							icon: 'none'
						});
						return;
					}
					
					uni.showToast({
						title: '登录成功',
						icon: 'success'
					});
					
					// 保存用户信息到本地存储
					uni.setStorageSync('userInfo', {
						phone: this.formData.phone,
						...res.data
					});
					
					// 登录成功后初始化WebSocket连接
					websocket.init(this.formData.phone);
					
					// 跳转到主页
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/index/index'
						});
					}, 1000);
				} catch (err) {
					console.error('登录失败:', err);
					uni.showToast({
						title: '网络请求失败',
						icon: 'none'
					});
				}
			},
			
			goToRegister() {
				uni.navigateTo({
					url: '/pages/register/register'
				});
			},
			
			forgetPassword() {
				uni.showToast({
					title: '忘记密码功能',
					icon: 'none'
				});
			}
		}
	}
</script>

<style>
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 100vh;
		padding: 0 40rpx;
		background-color: #f8f8f8;
	}
	
	.login-box {
		width: 100%;
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 60rpx 40rpx;
		box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.05);
	}
	
	.logo-section {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 60rpx;
	}
	
	.logo {
		width: 120rpx;
		height: 120rpx;
		margin-bottom: 30rpx;
	}
	
	.title {
		font-size: 48rpx;
		color: #333333;
		font-weight: bold;
	}
	
	.input-section {
		margin-bottom: 50rpx;
	}
	
	.input-item {
		margin-bottom: 30rpx;
	}
	
	.input-label {
		display: block;
		font-size: 28rpx;
		color: #666666;
		margin-bottom: 15rpx;
	}
	
	.input {
		width: 100%;
		height: 80rpx;
		background-color: #f8f8f8;
		border-radius: 10rpx;
		padding: 0 20rpx;
		font-size: 28rpx;
		box-sizing: border-box;
	}
	
	.login-button {
		width: 100%;
		height: 80rpx;
		background-color: #007AFF;
		color: #ffffff;
		font-size: 32rpx;
		border-radius: 10rpx;
		margin-bottom: 30rpx;
	}
	
	.footer {
		display: flex;
		justify-content: space-between;
	}
	
	.link {
		color: #007AFF;
		font-size: 26rpx;
	}
</style>